import { defineStore } from "pinia"
import { addClass, getMainClass, delClass, editClass,getCommonClass } from "@/api/classList/index"
//引入消息提示
import { ElMessage } from 'element-plus'

export const setClass = defineStore("class", {
    state: () => ({

    }),
    actions: {
        async getMainClass(currentPage: number, pageSize: number) {
            let res = await getMainClass(currentPage, pageSize)
            
            if (res.code == 200) {
                return Promise.resolve(res.data.courses)
            } else {
                return Promise.reject("获取信息失败")
            }
        },
        async getCommonClass(currentPage: number, pageSize: number){
            let res = await getCommonClass(currentPage, pageSize)
            
            if (res.code == 200) {
                return Promise.resolve(res.data.courses)
            } else {
                return Promise.reject("获取信息失败")
            }
        },
        async addClass(data: any) {
               
            let res = await addClass(data)
            if (res.code == 200) {
                ElMessage({
                    message: res.messages,
                    type: 'success',
                })
                return Promise.resolve(res.messages)
            } else {
                ElMessage({
                    message: res.messages,
                    type: 'error',
                })
                return Promise.reject(res.messages)
            }
        },
        async editClass(data: any) {
            let res = await editClass(data)
            if (res.code == 200) {
                ElMessage({
                    message: res.messages,
                    type: 'success',
                })
                return Promise.resolve(res.messages)
            } else {
                ElMessage({
                    message: res.messages,
                    type: 'error',
                })
                return Promise.reject(res.messages)
            }
        },
        async  delClass(id: number) {

            let res = await delClass(id)
            if (res.code == 200) {
                ElMessage({
                    message: res.messages,
                    type: 'success',
                })
                return Promise.resolve(res.messages)
            } else {
                ElMessage({
                    message: res.messages,
                    type: 'error',
                })
                return Promise.reject(res.messages)
            }
        }
    },
    getters: {}
})